// const BASE_URL = 'http://localhost:8080'
const BASE_URL = '/api/'

export const request = (options: any) => {

    // console.log(options)

    if (!options.url) {
        return;
    }
    // console.log("请求参数："+options.url)
    // console.log("请求参数："+options.method)
    // console.log("请求参数："+options.data)

    const timestamp = Date.now(); // 获取当前时间戳
    const token = localStorage.getItem('token')
    const refreshToken = localStorage.getItem('refreshToken')

    if (!token) {
        uni.switchTab({ url: '/pages/login/login' })
    }

    // 所有分页都放在地址栏上（后端接口全局获取）
    let params = "";
    if (options.params) {
        params = "?pageNum=" + options.params.pageNum + "&pageSize=" + options.params.pageSize
    }

    return new Promise((resolve: any, reject: any) => {
        uni.request({
            url: BASE_URL + options.url + params,
            method: options.method || 'GET',
            header: { token: token },
            data: options.data || {},
            success: (res) => {
                const body: any = res.data;
                if (0 == body.code) {
                    resolve(body);
                } else {
                    uni.showToast({
                        title: body.msg,
                        icon: 'error'
                    })
                }
            },
            fail: (error) => {
                uni.showToast({
                    title: '系统异常',
                    icon: 'error'
                })
            }

        })
    });

}


export default request;